package com.vca.service.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vca.common.exception.ExceptionCodeEnum;
import com.vca.common.exception.VcaException;
import com.vca.common.model.user.UserCollection;
import com.vca.service.dao.user.UserCollectionDao;
import com.vca.service.service.UserCollectionService;
import com.vca.service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品点赞和收藏表 服务实现类
 * </p>
 *
 * @author chenBing
 * @since 2022-11-09
 */
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionDao, UserCollection> implements UserCollectionService {
    @Autowired
    private UserCollectionDao dao;

    @Autowired
    private UserService userService;

    /**
     * 根据商品id和商品类型查询收藏量
     *
     * @param productId 商品id
     * @param mainType  类型:0-课程 1=套课 2=讲座 3=展览 4=商品
     * @return
     */
    @Override
    public List<UserCollection> getAdminList(Integer productId, int mainType) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper();
        wrapper.eq(UserCollection::getProductId, productId);
        wrapper.eq(UserCollection::getMainType, mainType);
        wrapper.eq(UserCollection::getIsDeleted, 0);
        wrapper.eq(UserCollection::getType, "collect");
        return dao.selectList(wrapper);
    }

    /**
     * @Description:用户收藏课程/讲座/展览/商品通用API
     * @author:chenbing
     * @date 2022/12/1 17:18
     */
    @Override
    public JSONArray collect(String language, Integer type, Integer mainId) {
        Integer userId = userService.getUserId();
        JSONArray jsonArray = new JSONArray();
        if (userId == 0) {
//            throw new VcaException("登录信息已过期，请重新登录！");
            throw new VcaException(ExceptionCodeEnum.UNAUTHORIZED.getMessage());
        }
        UserCollection collection = new UserCollection();
        if (type==4){
            collection = getOne(new LambdaQueryWrapper<UserCollection>()
                    .eq(UserCollection::getProductId, mainId)
                    .eq(UserCollection::getMainType, type)
                    .eq(UserCollection::getType, "collect")
                    .eq(UserCollection::getUid, userId));
        }else {
            collection = getOne(new LambdaQueryWrapper<UserCollection>()
                    .eq(UserCollection::getMainId, mainId)
                    .eq(UserCollection::getMainType, type)
                    .eq(UserCollection::getType, "collect")
                    .eq(UserCollection::getUid, userId));
        }
        if (collection == null) {
            UserCollection userCollection = new UserCollection();
            userCollection.setUid(userId);
            if (type==4){
                userCollection.setProductId(mainId);
            }else {
                userCollection.setMainId(Long.valueOf(mainId));
            }
            userCollection.setType("collect");
            userCollection.setMainType(type);
            if (save(userCollection)) {
                if("en".equals(language)){
                    jsonArray.add(0,1);
                    jsonArray.add(1,"Completed");
                    return jsonArray;
                    //return "Successful collection";
                }else{
                    jsonArray.add(0,1);
                    jsonArray.add(1,"收藏成功");
                    return jsonArray;
                    //return "收藏成功";
                }

            }else {
                if("en".equals(language)){
                    throw new VcaException("Failed");
                }else{
                    throw new VcaException("收藏失败");
                }
            }
        }else {
            if (collection.getIsDeleted() == 1) {
                collection.setIsDeleted(0);
                if (saveOrUpdate(collection)) {
                    if("en".equals(language)){
                        jsonArray.add(0,1);
                        jsonArray.add(1,"Completed");
                        return jsonArray;
                        //return "Successful collection";
                    }else{
                        jsonArray.add(0,1);
                        jsonArray.add(1,"收藏成功");
                        return jsonArray;
                        //return "收藏成功";
                    }

                }else {
                    if("en".equals(language)){
                        throw new VcaException("Failed");
                    }else{
                        throw new VcaException("收藏失败");
                    }
                }
            }
            if (collection.getIsDeleted() == 0) {
                collection.setIsDeleted(1);
                if (saveOrUpdate(collection)){
                    if("en".equals(language)){
                        jsonArray.add(0,0);
                        jsonArray.add(1,"Completed");
                        return jsonArray;
                        //return "Uncollect successfully";
                    }else{
                        jsonArray.add(0,0);
                        jsonArray.add(1,"取消收藏成功");
                        return jsonArray;
                        //return "取消收藏成功";
                    }

                }else {
                    if("en".equals(language)){
                        throw new VcaException("Failed");
                    }else{
                        throw new VcaException("取消收藏失败");
                    }

                }
            }
        }
        if("en".equals(language)){
            jsonArray.add(0,-1);
            jsonArray.add(1,"Failed");
            return jsonArray;
            //return "The favorites/unfavorites operation failed. Procedure";
        }else{
            jsonArray.add(0,-1);
            jsonArray.add(1,"收藏/取消收藏操作失败");
            return jsonArray;
            //return "收藏/取消收藏操作失败";
        }
    }
}
